home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_078 / mandelvroom / palette.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  10KB  |  383 lines

  1. /***************************************************************************
  2.  *
  3.  *                      MandleVroom Color Palette
  4.  *
  5.  *                         Kevin L. Clague
  6.  *
  7.  *                        Copyright (C) 1987
  8.  *
  9.  **************************************************************************/
  10.  
  11. #include "mand.h"
  12.  
  13. extern USHORT CurContour;
  14.  
  15. extern struct Gadget *ContGadget[], *SelGadget[];
  16.  
  17. extern struct Window *ContWind, *MandWind;
  18.  
  19. /*
  20.  * Holder for Allocated color potentiometer gadgets.
  21.  */
  22. struct ColorGads {
  23.   struct Gadget *RedPot;
  24.   struct Gadget *GreenPot;
  25.   struct Gadget *BluePot;
  26. };
  27.  
  28. extern struct Screen        *screen;
  29. extern struct ViewPort      *vp;
  30. extern struct IntuitionBase *IntuitionBase;
  31. extern char                 *ColorBase;
  32. extern struct IntuiText     *MakeIntui();
  33.  
  34. struct Window          *PalWind;
  35. extern SHORT XScale, YScale;
  36.  
  37. extern struct Window   *ContWind;
  38.  
  39. struct ColorGads        PalGads;
  40.  
  41. USHORT CmdMode;
  42. USHORT CurPen;
  43.  
  44. struct NewWindow NewPal = {
  45.    122,0,                    /* start position           */
  46.    90,120,                   /* width, height            */
  47.    (UBYTE) 0, (UBYTE) 1,     /* detail pen, block pen    */
  48.                              /* IDCMP flags */
  49.    MENUPICK | GADGETDOWN | GADGETUP | MOUSEMOVE | REQCLEAR| CLOSEWINDOW,
  50.                              /* MandWind flags */
  51.    WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH | SIMPLE_REFRESH | NOCAREREFRESH,
  52.    (struct Gadget *) NULL,   /* first gadget             */
  53.    (struct Image *) NULL,    /* user checkmark           */
  54.    (UBYTE *) "Colors",       /* window title             */
  55.    (struct Screen *) NULL,   /* pointer to screen        */
  56.    (struct BitMap *) NULL,   /* pointer to superbitmap   */
  57.    80,80,80,80,              /* sizing                   */
  58.    CUSTOMSCREEN              /* type of screen           */
  59.    };
  60.  
  61. extern struct Windows *WindowList;
  62.  
  63. /*
  64.  * Interpret the gadgets and do the right thing
  65.  */
  66. DoPalette(gadget)
  67.   struct Gadget *gadget;
  68. {
  69.   switch (gadget->GadgetID >> TYPEBITS & TYPEMASK) {
  70.  
  71.   case PALPENS:
  72.        ColorPenGotten(gadget);
  73.        SetNormPointer();
  74.        break;
  75.  
  76.   case PALCNTLS:
  77.        ColorCntlGotten(gadget);
  78.        break;
  79.  
  80.   case PALPOTS:
  81.        SetNormPointer();
  82.        CmdMode = COLORSLIDE;
  83.        break;
  84.   }
  85. } /* DoPalette */
  86.  
  87. /*
  88.  * We got a color palette command gadget
  89.  */
  90. int ColorCntlGotten(gadget)
  91. struct Gadget *gadget;
  92. {
  93.   switch (gadget->GadgetID) {
  94.  
  95.   case PALCOPY:
  96.        SetToPointer();
  97.        CmdMode = COPY;
  98.        break;
  99.  
  100.   case PALRANGE:
  101.        SetToPointer();
  102.        CmdMode = RANGE;
  103.        break;
  104.  
  105.   case PALEXCG:
  106.        SetToPointer();
  107.        CmdMode = EXCG;
  108.        break;
  109.   }
  110. } /* ColorCntlGotten */
  111.  
  112. /*
  113.  * We got a color pen. Now what was the command we were supposed to perform?
  114.  */
  115. int ColorPenGotten(gadget)
  116. struct Gadget *gadget;
  117. {
  118.   USHORT rgb, rgb2, pen;
  119.  
  120.   struct Image *Image;
  121.  
  122.   BoxPen((USHORT) CurPen, 0);
  123.  
  124.   pen = gadget->GadgetID & 0x003f;
  125.  
  126.   switch (CmdMode) {
  127.     case COPY:
  128.          rgb = GetRGB4(vp->ColorMap, CurPen);
  129.          SetRGB4(vp, pen, rgb >> 8, rgb >> 4, rgb);
  130.          break;
  131.  
  132.     case RANGE:
  133.          ColorRange(CurPen, pen);
  134.          break;
  135.  
  136.     case EXCG:
  137.          rgb  = GetRGB4(vp->ColorMap, CurPen);
  138.          rgb2 = GetRGB4(vp->ColorMap, pen);
  139.          SetRGB4(vp, CurPen, rgb2 >> 8, rgb2 >> 4, rgb2 );
  140.          SetRGB4(vp, pen,       rgb  >> 8, rgb  >> 4, rgb  );
  141.          break;
  142.  
  143.     case IMPLIEDSET:
  144.          Image = (struct Image *) SelGadget[ CurContour ]->GadgetRender;
  145.          Image->PlaneOnOff = pen;
  146.  
  147.          RefreshGList( SelGadget[ CurContour ], ContWind, NULL, 1);
  148.  
  149.          *(ColorBase + CurContour) = pen;
  150.          break;
  151.   }
  152.   CurPen = pen;
  153.   SetColorProps( pen );
  154.   BoxPen( pen, 1);
  155. } /* ColorPenGotten */
  156.  
  157. /*
  158.  * Blend a range of colors between two pens
  159.  */
  160. int ColorRange(first, last)
  161. SHORT first, last;
  162. {
  163.     SHORT i;
  164.     LONG whole, redfraction, greenfraction, bluefraction;
  165.     USHORT rgb;
  166.     SHORT firstred, firstgreen, firstblue;
  167.     SHORT lastred, lastgreen, lastblue;
  168.     SHORT workred, workgreen, workblue;
  169.  
  170.     if (first > last) {
  171.         i = first;
  172.         first = last;
  173.         last = i;
  174.      }
  175.  
  176.     /* I need to see a spread of at least two, where there's at least one
  177.      * spot between the endpoints, else there's no work to do so I
  178.      * might as well just return now.
  179.      */
  180.     if (first >= last - 1) return;
  181.  
  182.     rgb = GetRGB4(vp->ColorMap, first);
  183.     firstred = (rgb >> 8) & 0xF;
  184.     firstgreen = (rgb >> 4) & 0xF;
  185.     firstblue = (rgb >> 0) & 0xF;
  186.  
  187.     rgb = GetRGB4(vp->ColorMap, last);
  188.     lastred = (rgb >> 8) & 0xF;
  189.     lastgreen = (rgb >> 4) & 0xF;
  190.     lastblue = (rgb >> 0) & 0xF;
  191.  
  192.     whole = (lastred - firstred) << 16;
  193.     redfraction = whole / (last - first);
  194.     whole = (lastgreen - firstgreen) << 16;
  195.     greenfraction = whole / (last - first);
  196.     whole = (lastblue - firstblue) << 16;
  197.     bluefraction = whole / (last - first);
  198.  
  199.     for (i = first + 1; i < last; i++)
  200.         {
  201.         lastred = (redfraction * (i - first) + 0x8000) >> 16;
  202.         workred = firstred + lastred;
  203.         lastgreen = (greenfraction * (i - first) + 0x8000) >> 16;
  204.         workgreen = firstgreen + lastgreen;
  205.         lastblue = (bluefraction * (i - first) + 0x8000) >> 16;
  206.         workblue = firstblue + lastblue;
  207.         SetRGB4(vp, i, workred, workgreen, workblue);
  208.         }
  209. } /* ColorRange */
  210.  
  211. /*
  212.  * Modify the colors in the current pen
  213.  */
  214. ModifyColors()
  215. {
  216.   USHORT newred, newgreen, newblue;
  217.  
  218.   newred = ((struct PropInfo *)
  219.            PalGads.RedPot->SpecialInfo)->VertPot >> 12;
  220.   newgreen = ((struct PropInfo *)
  221.            PalGads.GreenPot->SpecialInfo)->VertPot >> 12;
  222.   newblue = ((struct PropInfo *)
  223.            PalGads.BluePot->SpecialInfo)->VertPot >> 12;
  224.  
  225.   SetRGB4(vp, CurPen, newred, newgreen, newblue);
  226. } /* ModifyColors */
  227.  
  228. /*
  229.  * Reflect a pen's new color in the proportional gadget
  230.  */
  231. SetColorProps(pen)
  232.   USHORT pen;
  233. {
  234.   USHORT rgb, red, green, blue;
  235.  
  236.   rgb = GetRGB4(vp->ColorMap, pen);
  237.  
  238.   red = (rgb >> 8) & 0xF;
  239.   green = (rgb >> 4) & 0xF;
  240.   blue = (rgb >> 0) & 0xF;
  241.  
  242.   red = (red << 12);
  243.   green = (green << 12);
  244.   blue = (blue << 12);
  245.  
  246.   ModifyProp(PalGads.RedPot,  PalWind,NULL,FREEVERT|PROPBORDERLESS,
  247.              0L,red,0L,0L);
  248.   ModifyProp(PalGads.GreenPot,PalWind,NULL,FREEVERT|PROPBORDERLESS,
  249.              0L,green,0L,0L);
  250.   ModifyProp(PalGads.BluePot, PalWind,NULL,FREEVERT|PROPBORDERLESS,
  251.              0L,blue,0L,0L);
  252. } /* SetColorProps */
  253.  
  254. BoxPen(BoxPen, DrawPen)
  255.   USHORT BoxPen, DrawPen;
  256. {
  257.   ULONG  row, column;
  258.   SHORT  Top, Bot, Left, Right;
  259.  
  260. #define PALTOP    (12 << YScale)
  261. #define PALLEFT   (4 << XScale)
  262. #define PENWIDTH  (4 << XScale)
  263. #define PENHEIGHT (4 << YScale)
  264. #define PENXPITCH (6 << XScale)
  265. #define PENYPITCH (6 << YScale)
  266.  
  267.   column = BoxPen/8;
  268.  
  269.   row = BoxPen - column*8;
  270.  
  271.   SetAPen(PalWind->RPort, DrawPen);
  272.  
  273.   Left  = PALLEFT + PENXPITCH * column - 1;
  274.   Top   = PALTOP  + PENYPITCH * row    - 1;
  275.   Right = Left    + PENWIDTH  + 1;
  276.   Bot   = Top     + PENHEIGHT + 1;
  277.  
  278.   Move(PalWind->RPort, Left,  Top);
  279.   Draw(PalWind->RPort, Right, Top);
  280.   Draw(PalWind->RPort, Right, Bot);
  281.   Draw(PalWind->RPort, Left,  Bot);
  282.   Draw(PalWind->RPort, Left,  Top + 1);
  283. }
  284.  
  285. /*
  286.  * Allocate all the gadgets for the color palette window
  287.  */
  288. struct Gadget *MakePalette()
  289. {
  290.   struct Gadget *FirstGadget, *NextGadget;
  291.   extern struct Gadget *MakeBool(), *MakePot();
  292.   SHORT i,Left,x,y,c = 0;
  293.  
  294.   FirstGadget = NextGadget = MakeBool(4,12,5,5,0,PALPEN);
  295.  
  296.   i = 1 << (screen->BitMap.Depth);
  297.   Left = 8;
  298.   for (x = 0; x < 6*8 && i > 0; x += 6) {
  299.     for (y = 0; y < 6*8 && i > 0; y += 6) {
  300.       if (c != 0) {
  301.         NextGadget->NextGadget = MakeBool(x+4, y+12, 4, 4, c, PALPEN+c);
  302.         if ((NextGadget = NextGadget->NextGadget) == (struct Gadget *) NULL)
  303.           return(FirstGadget);
  304.       }
  305.       c++;
  306.       i--;
  307.     }
  308.     Left += 8;
  309.   }
  310.   for (c = 0, x = 0; x < 10*3; x += 10) {
  311.     NextGadget->NextGadget = MakeBool(Left+36, x+12, 10, 6, 1, PALCNTL+c);
  312.     if (NextGadget->NextGadget == (struct Gadget *) NULL)
  313.       return(FirstGadget);
  314.     switch (c) {
  315.       case 0:  NextGadget->NextGadget->GadgetText =
  316.                   MakeIntui("Copy",14,-1,3,3,JAM1);
  317.                break;
  318.       case 1:  NextGadget->NextGadget->GadgetText =
  319.                   MakeIntui("Spread",14,-1,3,3,JAM1);
  320.                break;
  321.       case 2:  NextGadget->NextGadget->GadgetText =
  322.                   MakeIntui("Exchg",14,-1,3,3,JAM1);
  323.                break;
  324.     }
  325.     NextGadget = NextGadget->NextGadget;
  326.     c++;
  327.   }
  328.   for (x = y = 0; y < 3; x += 10, y++) {
  329.     NextGadget->NextGadget = MakePot(Left+x, 22, 5, 42, PALPOT + y, y);
  330.     if (NextGadget->NextGadget == (struct Gadget *) NULL)
  331.       return(FirstGadget);
  332.     switch (y) {
  333.       case 0:  PalGads.RedPot = NextGadget->NextGadget;
  334.                NextGadget->NextGadget->GadgetText =
  335.                  MakeIntui("R",-2,-10,3,3,JAM1);
  336.                break;
  337.       case 1:  PalGads.GreenPot = NextGadget->NextGadget;
  338.                NextGadget->NextGadget->GadgetText =
  339.                  MakeIntui("G",-2,-10,3,3,JAM1);
  340.                break;
  341.       case 2:  PalGads.BluePot = NextGadget->NextGadget;
  342.                NextGadget->NextGadget->GadgetText =
  343.                  MakeIntui("B",-2,-10,3,3,JAM1);
  344.                break;
  345.     }
  346.     NextGadget = NextGadget->NextGadget;
  347.     NextGadget->Activation = GADGIMMEDIATE | FOLLOWMOUSE | RELVERIFY;
  348.   }
  349.   return(FirstGadget);
  350. } /* MakePalette */
  351.  
  352. /*
  353.  * Open the Palette window
  354.  */
  355. OpenPalWind()
  356. {
  357.   struct Window *OpenMyWind();
  358.  
  359.   if (PalWind == (struct Window *) NULL) {
  360.     PalWind = OpenMyWind(&NewPal,screen,MakePalette(),140,70,140,70);
  361.     if (PalWind) {
  362.       CurPen = 0;
  363.       BoxPen(CurPen, 1);
  364.       SetColorProps( CurPen );
  365.       ForceNormPointer();
  366.     }
  367.   } else {
  368.     WindowToFront( PalWind );
  369.   }
  370. } /* OpenPalWind */
  371.  
  372. /*
  373.  * Close the Palette window
  374.  */
  375. ClosePalWind()
  376. {
  377.   if (PalWind != (struct Window *) NULL) {
  378.     CloseMyWind(PalWind,NewPal.FirstGadget);
  379.     PalWind = (struct Window *) NULL;
  380.   }
  381. } /* ClosePalWind */
  382.  
  383.